#include<libavutil/log.h>
#include<libavformat/avformat.h>
#include <libavformat/avio.h>
#include<stdio.h>

#define ADTS_HEADER_LEN  7;
//输入头信息函数
void adts_header(char *szAdtsHeader, int dataLen){

    int audio_object_type = 2;
    int sampling_frequency_index = 7;
    int channel_config = 2;

    int adtsLen = dataLen + 7;

    szAdtsHeader[0] = 0xff;         //syncword:0xfff                          高8bits
    szAdtsHeader[1] = 0xf0;         //syncword:0xfff                          低4bits
    szAdtsHeader[1] |= (0 << 3);    //MPEG Version:0 for MPEG-4,1 for MPEG-2  1bit
    szAdtsHeader[1] |= (0 << 1);    //Layer:0                                 2bits 
    szAdtsHeader[1] |= 1;           //protection absent:1                     1bit

    szAdtsHeader[2] = (audio_object_type - 1)<<6;            //profile:audio_object_type - 1                      2bits
    szAdtsHeader[2] |= (sampling_frequency_index & 0x0f)<<2; //sampling frequency index:sampling_frequency_index  4bits 
    szAdtsHeader[2] |= (0 << 1);                             //private bit:0                                      1bit
    szAdtsHeader[2] |= (channel_config & 0x04)>>2;           //channel configuration:channel_config               高1bit

    szAdtsHeader[3] = (channel_config & 0x03)<<6;     //channel configuration:channel_config      低2bits
    szAdtsHeader[3] |= (0 << 5);                      //original：0                               1bit
    szAdtsHeader[3] |= (0 << 4);                      //home：0                                   1bit
    szAdtsHeader[3] |= (0 << 3);                      //copyright id bit：0                       1bit  
    szAdtsHeader[3] |= (0 << 2);                      //copyright id start：0                     1bit
    szAdtsHeader[3] |= ((adtsLen & 0x1800) >> 11);           //frame length：value   高2bits

    szAdtsHeader[4] = (uint8_t)((adtsLen & 0x7f8) >> 3);     //frame length:value    中间8bits
    szAdtsHeader[5] = (uint8_t)((adtsLen & 0x7) << 5);       //frame length:value    低3bits
    szAdtsHeader[5] |= 0x1f;                                 //buffer fullness:0x7ff 高5bits
    szAdtsHeader[6] = 0xfc;
}

int main(int argc, char const *argv[])
{     
      int ret;
      int len;
      char *src=NULL;
      char *dest=NULL;
      int audio_index;
      FILE *dest_fd=NULL;
      AVFormatContext *ctx=NULL;
      AVPacket pct;
      av_log_set_level(AV_LOG_DEBUG);
      av_register_all();
      int err_code;
      char errors[1024];
      AVFrame *frame = NULL;
     
      if (argc<3)
      {
              av_log(NULL,AV_LOG_ERROR,"Argc is Error ");
              return -1;
      }

      src=argv[1];
      dest=argv[2];
      
      if (!src||!dest)
      {
        av_log(NULL,AV_LOG_ERROR,"Src or Dest is Null\n");
        return -1;
      }else
      {
        av_log(NULL,AV_LOG_ERROR,"-------> src: %s \n",src);
        av_log(NULL,AV_LOG_ERROR,"-------> dest: %s \n",dest);
      }
      
     //打开dest文件
      dest_fd= fopen(dest,"wb");
      if (!dest_fd)
      {
         av_log(NULL,AV_LOG_ERROR,"Open Dest is Error");
      
         avformat_close_input(&ctx);
         return -1;
      }
      
        
      //读取音频
      ret=avformat_open_input(&ctx, src,NULL,NULL);
      if (ret<0)
      {
        av_log(NULL,AV_LOG_ERROR,"-------> %s \n",av_err2str(ret));
        return -1;
      }
      


     /*读取音频流*/
    if((err_code = avformat_find_stream_info(ctx, NULL)) < 0) {
        av_strerror(err_code, errors, 1024);
        av_log(NULL, AV_LOG_DEBUG, "failed to find stream information: %s, %d(%s)\n",
               src,
               err_code,
               errors);
        return -1;
    }
     /*查看音频信息*/
     av_dump_format(ctx,0, src,0); 
     frame = av_frame_alloc();
     if(!frame){
        av_log(NULL, AV_LOG_DEBUG, "Could not allocate frame\n");
        return AVERROR(ENOMEM);
    }

    
      //寻找打开的最适合的流
       ret=av_find_best_stream(ctx,AVMEDIA_TYPE_AUDIO,-1,-1,NULL,0);
       if (ret<0)
       {
       av_log(NULL,AV_LOG_ERROR,"v_find_best_stream Error");
       avformat_close_input(&ctx);
       return -1;
       }
       audio_index=ret;
       
     //获取流内的每一个包  
      av_init_packet(&pct);
       pct.data = NULL;
       pct.size = 0;
    while (av_read_frame(ctx,&pct)>=0)
    {
        if (pct.stream_index==audio_index)
        {
          char adts_header_buf[7];
          adts_header(adts_header_buf,pct.size);
          fwrite(adts_header_buf,1,7,dest_fd);
           len = fwrite(pct.data,1,pct.size,dest_fd);
            if (len!=pct.size)
            {
                av_log(NULL,AV_LOG_ERROR,"pct .size is not equls");
            }
        }
        // av_packet_unref(&pct);
    }

   //回收操作
    avformat_close_input(&ctx);

    if (dest_fd)
    {
    fclose(dest_fd);
    }
    return 0;
}